package com.sss.hloster0508;

/**
 * 控制共享数据
 * 
 * @author 茶楼 关键字：：：synchronized
 */
public class ShareData02 {
	static int data = 0;
//定义一个锁对象 lock
	static final Object lock = new Object();

	public static void main(String[] args) {
		ShareThread01 st01 = new ShareThread01();
		ShareThread02 st02 = new ShareThread02();
		new Thread(st01).start();
		new Thread(st02).start();

	}

	private static class ShareThread01 implements Runnable {
		public void run() {
			// 对lock对象上锁
			synchronized (lock) {
				while (data < 10) {
					try {
						Thread.sleep(1000);
						System.out.println("这个小于10 的数据是：：" + (data++));
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}

	private static class ShareThread02 implements Runnable {
		public void run() {
			// 对lock对象上锁
			synchronized (lock) {

				while (data < 100) {
					
					System.out.println("此时的数据为：："+data);
					data++;
				}
			}
		}
	}
}